/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package candidate.beans;

import imageProcessing.classes.GeneralBufferedImageMaker;
import ProcesamientoImagenes.interfaces.BufferedImageMaker;
import facades.CandidateBookImageFacade;
import entities.Candidate;
import entities.CandidateBookImage;
import util.ImageStoreHelper;
import util.PrimeFacesBufferedImage;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.inject.Named;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import org.primefaces.event.FileUploadEvent;
import qualifiers.LoggedIn;

/**
 *
 * @author damfercer
 */
@Named(value = "candidateBookImagesBean")
@SessionScoped
public class CandidateBookImagesBean implements Serializable {
    @Inject
    CandidateBookImageFacade bookImageEJB;
    @Inject
    @LoggedIn
    private Candidate candidate;
    private CandidateBookImage selectedPicture;
    /**
     * Creates a new instance of CandidateBookImagesBean
     */
    public CandidateBookImagesBean() {
        Boolean a=true;
    }
    
    
    public void uploadBookImage(FileUploadEvent event) throws IOException{ 
        BufferedImageMaker imageMaker=new GeneralBufferedImageMaker();
        BufferedImage originalSizeImage=imageMaker.getBufferedImageFromInputStream(event.getFile().getInputstream());
        Integer maxPixels=800;
        PrimeFacesBufferedImage originalImageResized=new PrimeFacesBufferedImage(this.getScaledImage(originalSizeImage,maxPixels));
        String fileName=ImageStoreHelper.storePersistentlyPrimeFacesBufferedImage(originalImageResized, getCandidate());
        CandidateBookImage image=new CandidateBookImage();
        image.setCandidate(getCandidate());
        image.setImage(fileName);
        bookImageEJB.create(image);
        getCandidate().getCandidateBookImageList().add(image);
    }
    
    public void removeImage(){
        bookImageEJB.remove(selectedPicture);
        getCandidate().getCandidateBookImageList().remove(selectedPicture);
    }
    
    private BufferedImage getScaledImage(BufferedImage originalSizeImage, Integer maxPixels){
        Integer originalWidt=new Integer(originalSizeImage.getWidth());
        Integer originalHeigh=new Integer(originalSizeImage.getHeight());
        Double originalWidth=originalWidt.doubleValue();
        Double originalHeight=originalHeigh.doubleValue();
        Double ratio=originalWidth/originalHeight;
        Integer resWidth=null;
        Integer resHeight=null;
        if(ratio>1){
            resWidth=maxPixels;
            resHeight=(int)Math.ceil(maxPixels/ratio);
        }
        else{
           resHeight=maxPixels;
            resWidth=(int)Math.ceil(maxPixels*ratio); 
        }
        BufferedImage scaledImage = new BufferedImage(resWidth, resHeight, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D graphics2D = scaledImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(originalSizeImage, 0, 0, resWidth, resHeight, null);
        graphics2D.dispose();
        return scaledImage;
    }

    /**
     * @return the candidate
     */
    public Candidate getCandidate() {
        return candidate;
    }

    /**
     * @return the selectedPicture
     */
    public CandidateBookImage getSelectedPicture() {
        return selectedPicture;
    }

    /**
     * @param selectedPicture the selectedPicture to set
     */
    public void setSelectedPicture(CandidateBookImage selectedPicture) {
        this.selectedPicture = selectedPicture;
    }

    /**
     * @param candidate the candidate to set
     */

}
